<?php

class AssignmentController extends AuthController
{
    public $layout='//layouts/column2';
    
	public function actionIndex()
	{
        $adminUser = new AdminUser;
        $adminUser->attachBehavior('AuthUser', new AuthUserBehavior);
		$dataProvider = new EMongoDocumentDataProvider($adminUser, array(
			'pagination'=>array(
				'pageSize'=>50,
			),
		));

		// Render the view
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	* Displays the authorization assignments for an user.
	*/
	public function actionUser($id)
	{
        $adminUser = AdminUser::model()->getById($id);
        $adminUser->attachBehavior('AuthUser', new AuthUserBehavior);
		$this->render('user', array(
            'model'=>$adminUser,
		));
	}
    
    public function actionAssign($userId)
    {
        if( ! empty($_POST['Assign']))
        {
            $authNames = $_POST['Assign'];
            $auth = Yii::app()->authManager;
            foreach($authNames as $authName => $opt)
            {
                $isAssigned = $auth->isAssigned($authName, $userId);
                if(empty($opt))
                {
                    if($isAssigned)
                        $auth->revoke($authName, $userId);
                }
                elseif( ! $isAssigned)
                    $auth->assign($authName, $userId);
            }    
            $auth->save();
            $this->redirect(array('index'));
        }    
    }
}
